package com.kedacom.general.service.impl;

import com.alibaba.fastjson.JSON;
import com.kedacom.general.param.RequestParam;
import com.kedacom.general.param.ServiceParam;
import com.kedacom.general.result.ResponseMessage;
import com.kedacom.general.service.RequestService;
import com.kedacom.general.util.HttpUtils;
import com.kedacom.general.util.RequestBodyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class RequestServiceImpl implements RequestService {
    @Value("${service.regionalismCode:220000000000}")
    private String regionalismCode;
    @Value("${service.networkCode:3}")
    private String networkCode;
    @Value("${service.fields:name}")
    private String fields;
    @Value("${service.resourceAddress:http://10.67.7.21:8666/transmit/beforeTransmit}")
    private String resourceAddress;
    @Override
    public ResponseMessage sendService(HttpServletRequest request) {
        try {
            // 获取请求参数体
            RequestParam requestParam = RequestBodyUtil.getRequestParam(request);
            ServiceParam serviceParam = new ServiceParam();
            serviceParam.setMessageId(UUID.randomUUID().toString().replaceAll("-",""));
            serviceParam.setVersion("1.0");
            ServiceParam.ParameterBean parameterBean = new ServiceParam.ParameterBean();
            parameterBean.setDataObjId(request.getHeader("resourceId"));
            parameterBean.setRegionalismCode(regionalismCode);
            parameterBean.setNetworkCode(networkCode);
            parameterBean.setFields(fields);
            ServiceParam.ParameterBean.PageBean pageBean = new ServiceParam.ParameterBean.PageBean();
            pageBean.setPageNo(1);
            pageBean.setPageSize(1);
            parameterBean.setPage(pageBean);
            ServiceParam.ParameterBean.ConditionBean conditionBean = new ServiceParam.ParameterBean.ConditionBean();
            conditionBean.setLogicalOperate("and");
            List<ServiceParam.ParameterBean.ConditionBean.KeyValueListBean> keyValueList=new ArrayList<>();
            ServiceParam.ParameterBean.ConditionBean.KeyValueListBean keyValueListBean = new ServiceParam.ParameterBean.ConditionBean.KeyValueListBean();
            keyValueListBean.setKey(fields);
            keyValueListBean.setRelationOperator("=");
            // 把请求参数体放入value
            keyValueListBean.setValue(JSON.toJSONString(requestParam));
            keyValueList.add(keyValueListBean);
            conditionBean.setKeyValueList(keyValueList);
            parameterBean.setCondition(conditionBean);
            serviceParam.setParameter(parameterBean);
            // 模拟调用总线服务接口
            String url;
            String resourceAddressParam = request.getHeader("resourceAddress");
            if (StringUtils.isNotBlank(resourceAddressParam)){
                url=resourceAddressParam;
            }else{
                url=resourceAddress;
            }
            String userCredential = request.getHeader("userCredential");
            String appCredential = request.getHeader("appCredential");
            log.info("********************************调用服务总线参数"+JSON.toJSONString(serviceParam));
            HashMap<String, String> map = new HashMap<>();
            map.put("userCredential",userCredential);
            map.put("appCredential",appCredential);
            ResponseMessage httpPostResult = HttpUtils.getHttpPostResult(url, JSON.toJSONString(serviceParam), map);
            return httpPostResult;

        } catch (IOException e) {
            log.error("读取url接口参数异常");
            e.printStackTrace();
            return new ResponseMessage("系统异常",null,500,"-1");
        }
    }

}
